#!/bin/bash
# description: Install tigervnc server for UOS desktop professional
# version: 0.0.1
# author: ZhangChiQian<zhangchiqian@uniontech.com>
# date: 2021-11-29

echo "+------------------------------------------+"
echo "|   开始安装Tigervnc server		              "
echo "|   安装过程中要多次输入管理员密码，请保持耐心       " 
echo "|   请使用当前帐户权限，不要使用sudo命令执行安装脚本！"
echo "+------------------------------------------+"



if dpkg -l tigervnc-common &>/dev/null && dpkg -l tigervnc-standalone-server &>/dev/null;then
    echo "tigervnc已经安装"
else
    echo "正在安装tigervnc"        
    sudo dpkg -i ./tigervnc*.deb
fi

if [ $? -ne 0 ];then
    echo "修复依赖关系"
    echo -e "\033[40;37m 是否使用离线依赖包修复依赖关系？(y/n) \033[0m"
    read  i
	case $i in
		n|N)
            echo -e "\033[40;37m 是否尝试使用仓库修复依赖关系？(y/n) \033[0m"
            read j
            case $j in 
                y|Y)
                    echo "正在使用仓库修复依赖关系："
			        sudo apt --fix-broken install
                    ;;
                n|N)
                    echo -e "\033[40;37m 选择不修复依赖关系，按任意键结束安装 \033[0m"
                    read j
                    exit 11
                    ;;
            esac
			;;	
		y|Y)
			echo "尝试使用离线依赖包修复依赖关系"
			sudo dpkg -i ./Dependent/*.deb 
			;;
		*)
			echo "输入错误，退出"
			exit 11 
			;;
	esac
fi

echo -e "\033[40;37m 请设置连接vnc桌面时的密码（输入两次6位以上密码） \033[0m"
declare -i n=0
vncpasswd
while [ $? -ne 0 ];do
    echo -e "\033[0;31m 两次密码输入不匹配，请重新设置！ \033[0m"
    if [ $n -gt 5 ];then
        echo -e "\033[0;31m 错误次数过多，安装失败！ \033[0m"
        echo -e "\033[40;37m 安装失败，按任意键退出 \033[0m"
        read i
        exit 11
    fi
    let n++
    vncpasswd
done

if [ ! -e ~/.vnc/passwd ];then
    echo -e "\033[40;37m 安装失败，按任意键退出 \033[0m"
    read i
    exit 11
fi

echo "设置tigervnc xstartup脚本"
cat > ~/.vnc/xstartup <<'EOF'
PATH=/usr/bin:/usr/sbin
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
export XKL_XMODMAP_DISABLE=1
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS=@im=fcitx

#__using_dde__
session=startdde

#__enable_clipboard_sync__
if [ -f /usr/bin/autocutsel ]
then
	    /usr/bin/autocutsel &
fi

#__deepin_uos_activator__
if [ -f /usr/bin/uos-activator ]
then
	    /usr/bin/uos-activator & 
fi

#__enable_fcitx__
if [ -f /usr/bin/fcitx ]
then
	    (sleep 15 && /usr/bin/fcitx) &
fi

exec dbus-launch $session
EOF
chmod +x ~/.vnc/xstartup

echo "设置tigervnc自启动服务"
if [ $USER != 'root' ];then
    cat > ./tigervnc.service << EOF
[Unit]
Description=VNC Server by TeknoTut
After=syslog.target network.target

[Service]
Type=oneshot
RemainAfterExit=yes
Environment=vncUSER=$USER   #以哪个用户运行该安装脚本就打开的是哪个用户的桌面
PIDFile=$HOME/.vnc/%H:1.pid
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=su - \${vncUSER} -c "/usr/bin/vncserver -kill :1 > /dev/null 2>&1 || :"
# vncserver运行时默认使用$HOME/.vnc/passwd这个作为密码文件
ExecStart=su - \${vncUSER} -c "/usr/bin/vncserver -depth 24 -geometry 1600x900 -IdleTimeout 600 -AlwaysShared -xstartup ~/.vnc/xstartup -localhost no -rfbport 5900 -autokill :1"
#ExecStop=su - \${vncUSER} -c "/usr/bin/vncserver -kill :1"
ExecStop=-/usr/bin/killall vncserver
ExecStop=-/usr/bin/killall Xtigervnc
ExecStop=-/usr/bin/killall startdde
[Install]
WantedBy=multi-user.target
EOF
else
    cat > ./tigervnc.service << EOF
[Unit]
Description=VNC Server by TeknoTut
After=syslog.target network.target

[Service]
Type=oneshot
RemainAfterExit=yes
Environment=vncUSER=root   #以哪个用户运行该安装脚本就打开的是哪个用户的桌面
PIDFile=/root/.vnc/%H:1.pid
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=-/usr/bin/vncserver -kill :1 > /dev/null 2>&1
# vncserver运行时默认使用$HOME/.vnc/passwd这个作为密码文件
ExecStart=su - \${vncUSER} -c "/usr/bin/vncserver -depth 24 -geometry 1600x900 -IdleTimeout 600 -AlwaysShared -xstartup ~/.vnc/xstartup -localhost no -rfbport 5900 -autokill :1"
#ExecStop=su - \${vncUSER} -c "/usr/bin/vncserver -kill :1"
ExecStop=-/usr/bin/killall vncserver
ExecStop=-/usr/bin/killall Xtigervnc
ExecStop=-/usr/bin/killall startdde
[Install]
WantedBy=multi-user.target
EOF
fi 

sudo mv ./tigervnc.service /lib/systemd/system/tigervnc.service

sudo systemctl daemon-reload
sudo systemctl enable tigervnc
sudo systemctl start tigervnc

if [ $? -eq 0 ];then
    echo -e "\033[40;37m 安装完成，按任意键退出 \033[0m"
    read i
    exit 0
fi
